<?php

namespace app\api\service;

use think\facade\Db;

class GroupGoodsService
{

    /**
     *  获取首页拼团活动商品
     * */
    public static function getHomeGroupGoods($store_id)
    {

        $group_activity_id = Db::name('group_activity')
                                ->where(['store_id' => $store_id, 'status' => 1])
                                ->whereBetweenTimeField('start_time','end_time')
                                ->value('id');

        if (empty($group_activity_id)){
            return  [];
        }

        $groupGoods = Db::name('group_goods')
                        ->alias('a')
                        ->field('a.id,a.main_image,a.goods_name,a.sell_price,a.group_price,a.total_sale')
                        ->join('group_activity_goods b','a.id=b.group_goods_id')
                        ->withAttr('main_image',function ($value){
                            return request()->domain() . $value;
                        })->where(['b.group_activity_id' => $group_activity_id])
                        ->order(['a.sort' => 'asc','a.total_sale' => 'desc','a.id' => 'desc'])
                        ->limit(5)
                        ->select();

       return $groupGoods;
    }

    /**
     *  获取首页拼团活动商品列表
     * */

    public static function getHotGroupGoodsList($group_activity_id)
    {
        $groupGoodsList = Db::name('group_goods')->field('id,goods_name,main_image,total_sale,sell_price,group_price')
            ->withAttr('main_image',function ($value){
                return request()->domain() . $value;
            })->where('group_activity_id',$group_activity_id)
            ->paginate(10);

        return $groupGoodsList;
    }



    /**
     *  获取首页拼团活动商品列表
     * */

    public static function getGroupGoodsList($group_activity_id)
    {
        $groupGoodsList = Db::name('group_goods')->field('id,goods_name,main_image,total_sale,sell_price,group_price')
                                    ->withAttr('main_image',function ($value){
                                        return request()->domain() . $value;
                                    })->where('group_activity_id',$group_activity_id)
                                    ->paginate(10);

        return $groupGoodsList;
    }


}